package views.category;

import org.intellij.lang.annotations.Language;
import tools.Hint;
import tools.Icons;
import utils.DBUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.Statement;

/*没问题√＜（＾－＾）＞*/

public class JobCategoryAddFrame extends JFrame {
    private JPanel panBtn = new JPanel();
    private JPanel panLab = new JPanel();
    private JLabel jL_categoryname = new JLabel("职业类型名称：");
    private JTextField jT_category_name = new JTextField();
    private JButton btn_add = new JButton("添加");
    private JButton btn_reset = new JButton("重置");
    private JLabel jL_title = new JLabel();
    Icons icons = new Icons();
    ImageIcon imageIcon = new ImageIcon("src/images/job_category.png");

    public JobCategoryAddFrame() {
        setTitle("添加职业类型");
        setSize(350, 250);
        setIconImage(imageIcon.getImage().getScaledInstance(80, 80, Image.SCALE_DEFAULT));
        setResizable(false); // 不可改变窗口大小
        // 获取屏幕大小和当前frame的大小
        Dimension thisScreen = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension thisFrame = this.getSize();
        // 使启动窗口位于屏幕的正中心
        setLocation((thisScreen.width - thisFrame.width) / 2,
                (thisScreen.height - thisFrame.height) / 2);
        // 设置单击窗口的【关闭】按钮时将发生相应的动作
        setDefaultCloseOperation(HIDE_ON_CLOSE);

        jL_title.setFont(new Font("TsangerYuMo W03", Font.BOLD, 26));
        jL_title.setHorizontalAlignment(JLabel.CENTER);
        jL_title.setForeground(new Color(255, 51, 51));
        jL_title.setText("添  加  职  业  类  型");

        jL_categoryname.setSize(100, 20);
        jL_categoryname.setLocation(50, 45);
        jL_categoryname.setHorizontalAlignment(JLabel.RIGHT);

        jT_category_name.setSize(100, 20);
        jT_category_name.setLocation(160, 45);
        String hint_categoryname = "请输入职业类型名称";
        jT_category_name.addFocusListener(new Hint(jT_category_name, hint_categoryname));

        panBtn.add(btn_add);
        panBtn.add(btn_reset);
        panLab.setLayout(null);

        panLab.add(jL_categoryname);
        panLab.add(jT_category_name);

        add(jL_title, BorderLayout.NORTH);
        add(panBtn, BorderLayout.SOUTH);
        add(panLab, BorderLayout.CENTER);

        btn_add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // String count2 = jTextField1.getText().trim();
                // int count = Integer.parseInt(count2);
                String categoryname = jT_category_name.getText().trim();

                if (categoryname.equals(hint_categoryname)) {
                    JOptionPane.showMessageDialog(null, "请输入名称再添加！", "错误", JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                } else {
                    Statement stmt = DBUtil.getStatement();
                    @Language("MySQL")
                    String sql = "select category_name from job_category where category_name="
                            + "'"
                            + categoryname
                            + "'";
                    String sql2 = "insert into job_category(category_name) value("
                            + "'"
                            + categoryname
                            + "')";
                    try {
                        ResultSet rs = stmt.executeQuery(sql);
                        if (!rs.next()) {
                            stmt.executeUpdate(sql2);
                            JOptionPane.showMessageDialog(null, "添加成功。", "成功", JOptionPane.PLAIN_MESSAGE, icons.getOkImage());
                            dispose();
                        } else {
                            JOptionPane.showMessageDialog(null, "职业类型已存在！", "错误", JOptionPane.ERROR_MESSAGE, icons.getErrorImage());
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        btn_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                jT_category_name.setText("");

            }
        });
    }

    public static void main(String[] args) {
        new JobCategoryAddFrame().setVisible(true);
    }
}
